package com.baizhi.springboot.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baizhi.springboot.dao.UserDao;
import com.baizhi.springboot.entity.User;
import com.baizhi.springboot.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    @Transactional(readOnly = true)
    public Map selectUserByPage(Integer page, Integer limit) {
        Integer offset=(page-1)*limit;
        List<User> users = userDao.selectUserByPage(offset, limit);
        Integer count = userDao.selectCount();
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", users);
        return map;
    }

    @Override
    public void updateUserStatus(Integer id) {
        userDao.updateUserStatus(id);
    }

    @Override
    public HSSFWorkbook daochu() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ExportParams params = new ExportParams("用户信息展示","sheet", ExcelType.HSSF);
        List<User> users = userDao.selectAllUser();
        return (HSSFWorkbook) ExcelExportUtil.exportExcel(params, User.class, users);
    }

    @Override
    public Map daoru(InputStream inputStream) throws Exception {
        ImportParams params=new ImportParams();
        params.setTitleRows(1);
        List<User> list = ExcelImportUtil.importExcel(inputStream, User.class, params);
        userDao.daoru(list);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("isdaoru", "导入成功");
        return map;
    }

    @Override
    public List<Map> getUserSex() {
        return userDao.getUserSex();
    }

    @Override
    public List<Map> getUserProvince() {
        return userDao.getUserProvince();
    }

    /*@Override
    public HSSFWorkbook daochu() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("user");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("user_id");
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("user_telphone");
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("user_password");
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("user_image");
        HSSFCell cell5 = row.createCell(4);
        cell5.setCellValue("user_nickname");
        HSSFCell cell6 = row.createCell(5);
        cell6.setCellValue("user_name");
        HSSFCell cell7 = row.createCell(6);
        cell7.setCellValue("user_sex");
        HSSFCell cell8 = row.createCell(7);
        cell8.setCellValue("user_autograph");
        HSSFCell cell9 = row.createCell(8);
        cell9.setCellValue("user_province");
        HSSFCell cell11 = row.createCell(9);
        cell11.setCellValue("user_city");
        HSSFCell cell12 = row.createCell(10);
        cell12.setCellValue("guru_id");
        HSSFCell cell13 = row.createCell(11);
        cell13.setCellValue("user_status");
        HSSFCell cell14 = row.createCell(12);
        cell14.setCellValue("user_create_date");

       List<User> users= userDao.selectAllUser();
       Integer number=1;
        for (User user : users) {
            HSSFRow row1 = sheet.createRow(number);
            Field[] Fields = User.class.getDeclaredFields();
            for (int i = 0; i < Fields.length; i++) {
                HSSFCell cell = row1.createCell(i);
                String methodName="get"+Fields[i].getName().substring(0,1).toUpperCase()+Fields[i].getName().substring(1);
                Method method = User.class.getMethod(methodName);
                Object invoke = method.invoke(user);
                cell.setCellValue(invoke+"");
            }
            number++;
        }

        return workbook;
    }*/

    /*@Override
    public Map daoru(InputStream inputStream) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ParseException {
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        //总行数
        int numberOfRows = sheet.getPhysicalNumberOfRows();
        ArrayList<User> list = new ArrayList<>();
        for (int i = 1; i < numberOfRows; i++) {
            HSSFRow row = sheet.getRow(i);
            User user = new User();
            Field[] fields = User.class.getDeclaredFields();
            for (int j = 1; j < fields.length; j++) {
                HSSFCell cell = row.getCell(j);
                String methodName="set"+fields[j].getName().substring(0,1).toUpperCase()+fields[j].getName().substring(1);
                Method method = User.class.getMethod(methodName, (Class)fields[j].getGenericType());
                if ("status".equalsIgnoreCase(fields[j].getName())) {
                    System.out.println(methodName + "&&&&&&&&&&"+cell.getStringCellValue());
                   *//* System.out.println(methodName + "&&&&&&&&&&");
                    method.invoke(user, (int)cell.getNumericCellValue());*//*
                    method.invoke(user,Integer.parseInt(cell.getStringCellValue()));
//                   cell.getStringCellValue()
                } else if ("createDate".equals(fields[j].getName())) {

                    String stringCellValue = cell.getStringCellValue();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                    method.invoke(user, format.parse(stringCellValue));

                } else if ("guruId".equals(fields[j].getName())) {
                    *//*System.out.println(methodName + "---------"+(int)cell.getNumericCellValue());
                    method.invoke(user, (int)cell.getNumericCellValue());*//*
                    method.invoke(user,Integer.parseInt(cell.getStringCellValue()));
                } else {

                    System.out.println(methodName + "***************"+cell.getStringCellValue());
                    String stringCellValue = cell.getStringCellValue();
                    method.invoke(user, stringCellValue);
                }

            }
            System.out.println(user);
            list.add(user);
        }


        userDao.daoru(list);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("isdaoru", "导入成功");
        return map;
    }*/


}
